package com.company.jrlz.service;

import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Transaction;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.Sort;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service(SqlService.NAME)
public class SqlServiceBean implements SqlService {

    @Inject
    private Persistence persistence;

    @Inject
    private DataManager dataManager;

    @Override
    public void insertData(Object object) {
        try (Transaction ts = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            em.persist((Entity) object);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateData(Object object) {
        try (Transaction ts = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            em.merge((Entity) object);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteData(Object object) {
        try (Transaction ts = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            em.remove((Entity) object);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}